package indiwood.controller;

import indiwood.model.AdminVO;
import indiwood.model.MemberVO;
import indiwood.model.service.MemberService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
@RequestMapping("/member")
public class MemberController extends MultiActionController {
	@Resource(name = "memberServiceImpl")
	private MemberService memberService;
	@RequestMapping("/{viewId}_view.do")
	public String showView(@PathVariable("viewId") String viewId) {	
		return viewId+"_view.member";
	}
	@RequestMapping("/home.do")
	public String home(){
		return "home";
	}
	@RequestMapping(value="/login.do", method=RequestMethod.POST)
	public ModelAndView login(HttpServletRequest request, MemberVO mvo){
		String path="home";
		System.out.println(mvo);
		
		MemberVO result = memberService.login(mvo);
		System.out.println(result);
		if(result!=null){
			HttpSession session = request.getSession();
			//System.out.println(result);
			session.setAttribute("mvo", result);
		}else{
			path="login_fail.member";
		}
		return new ModelAndView(path);
	}
	@RequestMapping(value="/loginAdmin.do", method=RequestMethod.POST)
	public ModelAndView loginAdmin(HttpServletRequest request, AdminVO avo){
		String path="home";
		AdminVO result = memberService.loginAdmin(avo);
		if(result!=null){
			HttpSession session = request.getSession();
			session.setAttribute("avo", result);
		}else{
			path="login_fail.member";
		}
		return new ModelAndView(path);
	}
	@RequestMapping("/logout.do")
	public String logout(HttpServletRequest request){
		HttpSession session = request.getSession(false);
		if(session!=null){
			session.invalidate();
		}
		return "home";
	}
	@RequestMapping("/checkEmail.do")
	@ResponseBody
	public int Checkemail(String email){		
		int result=0;
		result = memberService.checkEmail(email);
		return result;
	}
	@RequestMapping("/register.do")
	public ModelAndView register(MemberVO mvo, String email_register, String password_register){
		mvo.setEmail(email_register);
		mvo.setPassword(password_register);
		memberService.register(mvo);
		return new ModelAndView("redirect:registerView.do?email="+mvo.getEmail());
	}
	@RequestMapping("/registerView.do")
	public ModelAndView registerView(String email){
		MemberVO mvo = memberService.findMemberByEmail(email);		
		return new ModelAndView("register_ok.member","mvo",mvo);
	}
	@RequestMapping("/updateMember.do")
	public ModelAndView updateMember(HttpServletRequest request, MemberVO mvo){
		MemberVO session = (MemberVO) request.getSession().getAttribute("mvo");
		mvo.setEmail(session.getEmail());
		memberService.updateMember(mvo);
		return new ModelAndView("redirect:updateView.do?email="+mvo.getEmail());
	}
	@RequestMapping("/updateView.do")
	public ModelAndView updateView(HttpServletRequest request, String email){
		MemberVO mvo = memberService.findMemberByEmail(email);
		if(mvo!=null){
			HttpSession session = request.getSession();
			session.setAttribute("mvo", mvo);
		}
		return new ModelAndView("home");
	}
	@RequestMapping("/outMember.do")
	public ModelAndView outMember(HttpServletRequest request){
		logout(request);
		MemberVO mvo = (MemberVO) request.getSession().getAttribute("mvo");
		memberService.outMember(mvo);		
		return new ModelAndView("redirect:home.do");
	}	
}
